import axios from 'axios';
import { AxiosError } from "axios";
import router from "@/router";

const domain = window.location.hostname;
console.log(domain);


const Axios = axios.create({
    baseURL: (domain == "localhost" ? 'http://localhost:8088/' : 'http://47.94.82.79:8088/'), // 设置默认 API 地址
});


Axios.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token');
        if (token) {
            config.headers.Authorization = `${token}`;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);


Axios.interceptors.response.use(response => {
    // console.log(response);
    // return Promise.reject(new AxiosError("未登录", "401", response.config, response.request, response));
    return response;
}, (error) => {
    // 打印错误信息
    window.$utils.message.error(error.message);
    console.log(error);
    if (error.code == 401) {
        window.$utils.message.error("未登录");
        router.push({ name: 'login' });
    }
    return Promise.reject(error);
});

export default Axios;
